import { changeNumMoneyToChinese, getColorTextLoadMap, getDeptPosTreeIcon, getKeyRouter, getUserShortCode } from '@/utils';
import moment from 'moment';
import { useEffect } from 'react';
import { IContractsActionEnum } from '../common';

/**
 * 处理JSON中日期格式
 * @param timeList
 * @param timeName
 * @returns
 */
const handListMoment = (timeList: Record<string, any>[], timeName: string[], momentType: boolean) => {
  let momentList: Record<string, any>[] = [];
  timeList?.forEach((itemData: any) => {
    let itemObj: Record<string, any> = { ...itemData };
    timeName?.forEach((itemCode: any) => {
      if (itemCode && itemObj?.[itemCode]) {
        if (momentType) {
          itemObj[itemCode] = moment(itemObj?.[itemCode])?.format('YYYY-MM-DD HH:mm:ss');
        } else {
          itemObj[itemCode] = moment(itemObj?.[itemCode]);
        }
      }
    });
    momentList?.push(itemObj);
  });

  return momentList;
};

/** 处理搜索组织机构部门 */
const handleDutySearchList = (selectData: any[], name: any, value: any) => {
  let loopList: any = (dataList: any[]) => {
    return dataList?.map((item: any) => {
      return {
        ...item,
        name: (
          <span>
            {getDeptPosTreeIcon(String(item?.textType))}
            {item?.[`${name}`]}
          </span>
        ),
        // name: item?.[`${name}`],
        treeName: item?.[name],
        value: item?.[`${value}`],
        children: item?.children && item?.children?.length > 0 ? loopList(item?.children) : null,
        disabled: item?.textType == 1 ? false : true,
      };
    });
  };
  let newMapList = loopList(selectData);
  return newMapList;
};

/** 处理税率对象 */
const taxRateInfoHand = (dataList: Record<string, any>[]) => {
  let taxRateInfo: Record<string, any> = {};
  dataList?.forEach((item: any) => {
    if (item?.name) {
      taxRateInfo[Number(item?.name)] = item?.name;
    }
  });
  return taxRateInfo;
};

// /** 处理业务部门,只有部门可选择 */
// const busicDeptData = (treeData: Record<string, any>[]) => {
//   const loopList: any = (dataList: any[]) => {
//     return dataList?.map((item: any) => {
//       return {
//         ...item,
//         children: item?.children && item?.children?.length > 0 ? loopList(item?.children) : null,
//         disabled: item?.textType == '1' ? false : true,
//       };
//     });
//   };
//   let newMapList = loopList(treeData);
//   return newMapList;
// };

/** 处理相对方名称 */
const handSupplierNames = (detailList: Record<string, any>[]) => {
  let namesStr: string[] = [];
  detailList?.forEach((item: any) => {
    namesStr?.push(item?.oppositeName);
  });
  return namesStr?.length > 0 ? namesStr?.toString() : null;
};

/** 处理主合同数据 */
const handMainContractData = (infoObj: Record<string, any>) => {
  const mainInfo: Record<string, any> = {
    ...infoObj,
    id: infoObj?.id ? infoObj?.id : null,
    supplyTypeName: getColorTextLoadMap(infoObj?.supplyTypeName)?.colorText,
    procureRealName: getColorTextLoadMap(infoObj?.procureRealName)?.colorText,
    stateName: getColorTextLoadMap(infoObj?.stateName)?.colorText,
    payStyleName: getColorTextLoadMap(infoObj?.payStyleName)?.colorText,
  };

  return mainInfo;
};

/** 处理变更合同编号 */
// const modifyContractCode = (info: Record<string, any>) => {
//   let typeCode: string = '';
//   if (info?.contractCode) {
//     typeCode = info?.contractCode?.substring(8, 10);
//   }
//   return `${getUserShortCode()}${typeCode}`;
// };

/** 处理变更List 去除ID，且modifyFlag 为添加 */
const modifyJsonListFlag = (list: Record<string, any>[], saveID?: boolean) => {
  let newList: Record<string, any>[] = [];
  list?.forEach((item: any) => {
    const obj: Record<string, any> = {
      ...item,
      modifyFlag: '0',
      key: `contractJson-${getKeyRouter()}`,
    };
    if (!saveID) {
      delete obj['id'];
    }
    newList?.push({
      ...obj,
    });
  });
  return newList;
};

/** 监听主合同信息 */
const effectMainContract = (
  oldContractInfo: Record<string, any>,
  contractForm: any,
  changeModifyItem: Record<string, any>,
  setChangeModifyItem: (obj: Record<string, any>) => void,
  contractDispatch: any,
  contarctJsonList: Record<string, any>,
  contractDetailInfo: Record<string, any>,
) => {
  useEffect(() => {
    if (oldContractInfo && oldContractInfo?.id) {
      const newDetailInfo: Record<string, any> = {
        oldContractName: oldContractInfo?.contractName ? oldContractInfo?.contractName : null,
        oldContractCode: oldContractInfo?.contractCode ? oldContractInfo?.contractCode : null,
        oldProjectCode: oldContractInfo?.projectCode ? oldContractInfo?.projectCode : null,
        oldProjectName: oldContractInfo?.projectName ? oldContractInfo?.projectName : null,
        oldPayStyle: oldContractInfo?.payStyle ? oldContractInfo?.payStyle : null,
        payStyle: oldContractInfo?.payStyle ? oldContractInfo?.payStyle : null,
        oldSumInTax: oldContractInfo?.sumInTax ? oldContractInfo?.sumInTax : null,
        sumInTax: oldContractInfo?.sumInTax ? oldContractInfo?.sumInTax : null,
        oldOppositeList: oldContractInfo?.oppositeList ? oldContractInfo?.oppositeList : null,
        oldDetailList: oldContractInfo?.detailList ? oldContractInfo?.detailList : null,
        oldContractInfo: oldContractInfo?.contractInfo ? oldContractInfo?.contractInfo : null,
        oldPreBeginTime: oldContractInfo?.preBeginTime ? moment(oldContractInfo?.preBeginTime) : null,
        oldPreFinishTime: oldContractInfo?.preFinishTime ? moment(oldContractInfo?.preFinishTime) : null,
        realBeginTime: oldContractInfo?.realBeginTime ? moment(oldContractInfo?.realBeginTime) : null,
        realFinishTime: oldContractInfo?.realFinishTime ? moment(oldContractInfo?.realFinishTime) : null,
        preBeginTime: oldContractInfo?.preBeginTime ? moment(oldContractInfo?.preBeginTime) : null,
        preFinishTime: oldContractInfo?.preFinishTime ? moment(oldContractInfo?.preFinishTime) : null,
        contractName: oldContractInfo?.contractName
          ? `${oldContractInfo?.contractName}${oldContractInfo?.changeWay == 0 ? '-变更' : oldContractInfo?.changeWay == 1 ? '-解除' : ''}`
          : null,
        contractCode: !oldContractInfo.pID ? null : oldContractInfo?.contractCode ? oldContractInfo?.contractCode : null,

        /** 合同主体信息 */
        oppositeName: oldContractInfo?.oppositeName ? oldContractInfo?.oppositeName : null,
        creditID: oldContractInfo?.creditID ? oldContractInfo?.creditID : null,
        bankName: oldContractInfo?.bankName ? oldContractInfo?.bankName : null,
        bankAccount: oldContractInfo?.bankAccount ? oldContractInfo?.bankAccount : null,
        workAddress: oldContractInfo?.workAddress ? oldContractInfo?.workAddress : null,
        registerPhone: oldContractInfo?.registerPhone ? oldContractInfo?.registerPhone : null,
        oppositeID: oldContractInfo?.oppositeID ? oldContractInfo?.oppositeID : null,
        contactName: oldContractInfo?.contactName ? oldContractInfo?.contactName : null,
        phoneMobile: oldContractInfo?.phoneMobile ? oldContractInfo?.phoneMobile : null,
        /** 合同招采信息 */
        // purchProjectName: oldContractInfo?.purchProjectName ? oldContractInfo?.purchProjectName : null,
        // purchProjectCode: oldContractInfo?.purchProjectCode ? oldContractInfo?.purchProjectCode : null,
        // bidTime: oldContractInfo?.bidTime ? oldContractInfo?.bidTime : null,
        // procureReal: oldContractInfo?.procureReal ? oldContractInfo?.procureReal : null,
        // applyID: oldContractInfo?.applyID ? oldContractInfo?.applyID : null,
        // supplyType: oldContractInfo?.supplyType ? oldContractInfo?.supplyType : null,
        // sourceID: oldContractInfo?.sourceID ? oldContractInfo?.sourceID : null,
        // applyName: oldContractInfo?.applyName ? oldContractInfo?.applyName : null,

        //合同币种 汇率
        oldCurrencyType: oldContractInfo?.currencyType ? oldContractInfo?.currencyType : null,
        currencyType: oldContractInfo?.currencyType ? oldContractInfo?.currencyType : null,
        oldCurrencyRate: oldContractInfo?.currencyRate ? oldContractInfo?.currencyRate : null,
        currencyRate: oldContractInfo?.currencyRate ? oldContractInfo?.currencyRate : null,
      };

      if (!contractDetailInfo?.id && oldContractInfo && oldContractInfo?.oppositeList?.length > 0) {
        const newOpposList: Record<string, any>[] = modifyJsonListFlag(oldContractInfo?.oppositeList, true);
        //保存选中相对方
        contractDispatch({
          type: IContractsActionEnum.SELECT_COUNTERPARTROWS,
          payload: newOpposList,
        });

        // 保存处理相对方数据传入接口
        contractDispatch({
          type: IContractsActionEnum.CONTRACT_JSONLIST,
          payload: {
            ...contarctJsonList,
            oppositeList: [...newOpposList],
          },
        });
      }

      // //判断是否有已付金额，有则支付方式不可更改
      if (oldContractInfo?.sumPaid && oldContractInfo?.sumPaid > 0) {
        setChangeModifyItem({
          ...changeModifyItem,
          isPayDisabled: true,
        });
      }

      contractForm?.setFieldsValue({
        ...newDetailInfo,
      });
    }
  }, [oldContractInfo?.id]);

  /** 判断收付款信息，供货清单初始化 */
  // useEffect(() => {
  //   if (oldContractInfo?.id) {
  //     defaultPayList(oldContractInfo?.processList, contractForm)
  //     defaultDetailList(oldContractInfo?.detailList, contractForm)
  //   }
  // }, [oldContractInfo?.processList, oldContractInfo?.detailList])
};

/** 一次性支付和其他支付方式修改支付金额处理数据 */
const changeContractSumList = (contractForm: any, contractDispatch: any, contarctJsonList: any, payMent) => {
  const payStyle = contractForm?.getFieldValue('payStyle');
  const sumInTax = contractForm?.getFieldValue('sumInTax');
  const contractCode = contractForm?.getFieldValue('contractCode');
  let currentList = contractForm?.getFieldValue('processList') || [];

  if (currentList?.length && sumInTax && (payStyle == `${payMent}1002` || payStyle == `${payMent}1000`)) {
    let showList: any = [];
    let changeList: any = [];
    let payItem: any = {};
    let jsonList = contarctJsonList?.['processList']?.length > 0 ? contarctJsonList?.['processList'] : [];

    currentList.forEach((item) => {
      if (item.payFlag) {
        payItem = item;
        item.paySum = sumInTax;
        showList.push({ ...item, paySum: sumInTax, modifyFlag: !item?.id ? 0 : contractCode ? 1 : 0 });
        if (!jsonList.length) {
          changeList.push({ ...item, paySum: sumInTax, modifyFlag: !item?.id ? 0 : contractCode ? 1 : 0 });
        }
      } else {
        showList.push(item);
      }
    });

    (jsonList || []).forEach((item) => {
      if (item.id == payItem?.id) {
        changeList.push({
          ...item,
          paySum: sumInTax,
          modifyFlag: !item?.id ? 0 : contractCode ? 1 : 0,
        });
      } else {
        changeList.push(item);
      }
    });

    if (!(changeList.filter((item) => item?.id == payItem?.id) || []).length) {
      changeList.push({
        ...payItem,
        paySum: sumInTax,
        modifyFlag: !payItem?.id ? 0 : contractCode ? 1 : 0,
      });
    }

    contractForm?.setFieldsValue({
      processList: showList?.length > 0 ? handListMoment(showList, ['payTime'], false) : [],
    });

    // // // 保存处理履约方式数据传入接口
    contractDispatch({
      type: IContractsActionEnum.CONTRACT_JSONLIST,
      payload: {
        ...contarctJsonList,
        processList: [...changeList],
      },
    });
  }

  //分期支付时，金额不调整，需手动输入
  if (currentList?.length && sumInTax && payStyle == `${payMent}1001`) {
    let showList: any = [];
    let changeList: any = [];
    const currentList = contractForm?.getFieldValue('processList') || [];
    const jsonList = contarctJsonList?.['processList']?.length > 0 ? contarctJsonList?.['processList'] : [];

    currentList.forEach((item) => {
      if (item.payFlag) {
        let payNum_ = (item?.paySum / sumInTax) * 100;

        showList.push({ ...item, payNum: payNum_ ? payNum_?.toFixed(2) : 0, modifyFlag: !item?.id ? 0 : contractCode ? 1 : 0 });
        const isHasArray = jsonList.filter((element) => element?.id == item?.id || element?.key == item?.key) || [];
        if (!isHasArray?.length) {
          changeList.push({
            ...item,
            // paySum: (sumInTax * item?.payNum) / 100 || 0,
            payNum: payNum_ ? payNum_?.toFixed(2) : 0,
            modifyFlag: !item?.id ? 0 : contractCode ? 1 : 0,
          });
        }
      } else {
        showList.push(item);
      }
    });

    (jsonList || []).forEach((element) => {
      let payNum_ = (element?.paySum / sumInTax) * 100;

      changeList.push({
        ...element,
        // paySum: (sumInTax * element?.payNum) / 100 || 0,
        payNum: payNum_ ? payNum_?.toFixed(2) : 0,
      });
    });

    (showList || []).forEach((item) => {
      let isHave: any = [];
      changeList.forEach((i) => {
        if (i?.id == item?.id) {
          isHave.push(item);
        }
      });
      if (!isHave?.length) {
        changeList.push(item);
      }
    });

    contractForm?.setFieldsValue({
      processList: showList?.length > 0 ? handListMoment(showList, ['payTime'], false) : [],
    });

    // // // 保存处理履约方式数据传入接口
    contractDispatch({
      type: IContractsActionEnum.CONTRACT_JSONLIST,
      payload: {
        ...contarctJsonList,
        processList: [...changeList],
      },
    });
  }
};

/** 一次性支付和其他支付方式修改支付金额处理数据 */
const changePayList = (contractForm: any, contractDispatch: any, contarctJsonList: any, payMent) => {
  const payStyle = contractForm?.getFieldValue('payStyle');
  const sumInTax = contractForm?.getFieldValue('sumInTax');
  const contractCode = contractForm?.getFieldValue('contractCode');

  if (sumInTax && (payStyle == `${payMent}1002` || payStyle == `${payMent}1000`)) {
    let showList: any = [];
    let changeList: any = [];
    let currentList = contractForm?.getFieldValue('processList') || [];
    let payItem: any = {};
    let jsonList = contarctJsonList?.['processList']?.length > 0 ? contarctJsonList?.['processList'] : [];

    currentList.forEach((item) => {
      if (item.payFlag) {
        payItem = item;
        item.paySum = sumInTax;
        showList.push({ ...item, paySum: sumInTax, modifyFlag: !item?.id ? 0 : contractCode ? 1 : 0 });
        if (!jsonList.length) {
          changeList.push({ ...item, paySum: sumInTax, modifyFlag: !item?.id ? 0 : contractCode ? 1 : 0 });
        }
      } else {
        showList.push(item);
      }
    });

    (jsonList || []).forEach((item) => {
      if (item.id == payItem?.id) {
        changeList.push({
          ...item,
          paySum: sumInTax,
          modifyFlag: !item?.id ? 0 : contractCode ? 1 : 0,
        });
      } else {
        changeList.push(item);
      }
    });

    if (!(changeList.filter((item) => item.id == payItem.id) || ([] && payItem?.id)).length) {
      changeList.push({
        ...payItem,
        paySum: sumInTax,
        modifyFlag: !payItem?.id ? 0 : contractCode ? 1 : 0,
      });
    }

    contractForm?.setFieldsValue({
      processList: showList?.length > 0 ? handListMoment(showList, ['payTime'], false) : [],
    });

    // // // 保存处理履约方式数据传入接口
    contractDispatch({
      type: IContractsActionEnum.CONTRACT_JSONLIST,
      payload: {
        ...contarctJsonList,
        processList: [...changeList],
      },
    });
  }

  //分期支付时，金额不调整，需手动输入
  if (sumInTax && payStyle == `${payMent}1001`) {
    let showList: any = [];
    let changeList: any = [];
    const currentList = contractForm?.getFieldValue('processList') || [];
    const jsonList = contarctJsonList?.['processList']?.length > 0 ? contarctJsonList?.['processList'] : [];

    currentList.forEach((item) => {
      if (item.payFlag) {
        let payNum_ = (item?.paySum / sumInTax) * 100;

        showList.push({ ...item, payNum: payNum_ ? payNum_?.toFixed(2) : 0, modifyFlag: !item?.id ? 0 : contractCode ? 1 : 0 });
        const isHasArray = jsonList.filter((element) => element?.id == item?.id || element?.key == item?.key) || [];
        if (!isHasArray?.length) {
          changeList.push({
            ...item,
            payNum: payNum_ ? payNum_?.toFixed(2) : 0,
            modifyFlag: !item?.id ? 0 : contractCode ? 1 : 0,
          });
        }
      } else {
        showList.push(item);
      }
    });

    (jsonList || []).forEach((element) => {
      let payNum_ = (element?.paySum / sumInTax) * 100;
      changeList.push({
        ...element,
        // paySum: (sumInTax * element?.payNum) / 100 || 0,
        payNum: payNum_ ? payNum_?.toFixed(2) : 0,
      });
    });

    (showList || []).forEach((item) => {
      let isHave: any = [];
      changeList.forEach((i) => {
        if (i?.id == item?.id) {
          isHave.push(item);
        }
      });
      if (!isHave?.length) {
        changeList.push(item);
      }
    });

    contractForm?.setFieldsValue({
      processList: showList?.length > 0 ? handListMoment(showList, ['payTime'], false) : [],
    });

    // // // 保存处理履约方式数据传入接口
    contractDispatch({
      type: IContractsActionEnum.CONTRACT_JSONLIST,
      payload: {
        ...contarctJsonList,
        processList: [...changeList],
      },
    });
  }
};

/** 修改支付方式处理数据初始化 */
const defaultPayList = (dataList: Record<string, any>[], contractForm: any, contractDispatch: any, contarctJsonList: any) => {
  const newProcessList: Record<string, any>[] = dataList?.length > 0 ? modifyJsonListFlag(dataList) : [];
  contractForm?.setFieldsValue({
    processList: newProcessList?.length > 0 ? handListMoment(newProcessList, ['payTime'], false) : [],
  });
  // 保存处理履约方式数据传入接口
  // contractDispatch({
  //   type: IContractsActionEnum.CONTRACT_JSONLIST,
  //   payload: {
  //     ...contarctJsonList,
  //     processList: [],
  //   },
  // });
};

/** 修改供货服务清单初始化数据 */
const defaultDetailList = (dataList: Record<string, any>[], contractForm: any, contractDispatch: any, contarctJsonList: any) => {
  const newDetailList: Record<string, any>[] = dataList?.length > 0 ? modifyJsonListFlag(dataList) : [];
  contractForm?.setFieldsValue({
    detailList: newDetailList?.length > 0 ? handListMoment(newDetailList, ['deliveryTime'], false) : [],
  });
  // 保存处理货物清单数据传入接口
  contractDispatch({
    type: IContractsActionEnum.CONTRACT_JSONLIST,
    payload: {
      ...contarctJsonList,
      detailList: [...newDetailList],
    },
  });
};

/** 判断收付款金额是否与填写金额一致，不一致不让保存 */
const contractNumAndPayNum = (processList: Record<string, any>[]) => {
  let contractNum: number = 0;
  processList?.forEach((item: any) => {
    // contractNum += item?.paySum ? item?.paySum : 0;
    contractNum += item?.payNum ? Number(item?.payNum) : 0;
  });
  return contractNum;
};

export function float_add(arg1, arg2) {
  var r1, r2, m, n;
  try {
    r1 = arg1.toString().split('.')[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split('.')[1].length;
  } catch (e) {
    r2 = 0;
  }
  m = Math.pow(10, Math.max(r1, r2));
  n = r1 >= r2 ? r1 : r2;
  return ((arg1 * m + arg2 * m) / m).toFixed(n);
}

/** 判断收付款金额是否与填写金额一致，不一致不让保存 */
const contractSumAndPaySum = (processList: Record<string, any>[]) => {
  let contractSum: any = 0;
  processList?.forEach((item: any) => {
    contractSum = float_add(contractSum, item?.paySum || 0);
  });
  return contractSum;
};

/** 处理数据去掉岗位 */
const handleNotPostion = (treeData: Record<string, any>[]) => {
  const loopTree = (dataList: Record<string, any>[]) => {
    dataList?.forEach((item: any, index: number) => {
      if (item?.textType == '0' || item?.textType == '1') {
        const childrenList: Record<string, any>[] = item?.children?.filter((itemChil: any) => itemChil?.textType == '0' || itemChil?.textType == '1');
        dataList[index] = {
          ...item,
          title: (
            <span>
              {getDeptPosTreeIcon(String(item?.textType))}
              {item?.text}
            </span>
          ),
          children: childrenList?.length > 0 ? loopTree(childrenList) : null,
          disabled: item?.textType == '0' ? true : false,
          // disabled: item?.dutyPositionID ? false : true
        };
      }
    });
    return dataList;
  };

  const treeList: Record<string, any>[] = loopTree(treeData);
  return treeList;
};

// 处理数据只显示单位
const handleAllDepartment = (treeData: Record<string, any>[]) => {
  const loopTree = (dataList: Record<string, any>[]) => {
    dataList?.forEach((item: any, index: number) => {
      if (item?.textType == '0') {
        const childrenList: Record<string, any>[] = item?.children?.filter((itemChil: any) => itemChil?.textType == '0');
        dataList[index] = {
          ...item,
          title: (
            <span>
              {getDeptPosTreeIcon(String(item?.textType))}
              {item?.text}
            </span>
          ),
          children: childrenList?.length > 0 ? loopTree(childrenList) : null,
          disabled: item?.textType == '1' ? true : false,
          // disabled: item?.dutyPositionID ? false : true
        };
      }
    });
    return dataList;
  };

  const treeList: Record<string, any>[] = loopTree(treeData);
  return treeList;
};

/** 处理在线编辑书签数据 */
const handleBookmark = (info: Record<string, any>, codeKeys: string[]) => {
  let bookMarkInfo: Record<string, any> = {};
  if (info && JSON.stringify(info) != '{}') {
    Object?.keys(info)?.forEach((item: any) => {
      if (codeKeys?.includes(item)) {
        codeKeys?.forEach((itemCode: any) => {
          info[itemCode]?.forEach((itemData: any) => {});
        });
      } else {
        bookMarkInfo[item] = bookMarkInfo[item];
      }
    });
  }

  return bookMarkInfo;
};

/** 合并相同物料 */
// const mergeMaterial = (dataList: Record<string, any>[]) => {
//   let materialInfo: Record<string, any> = {}
//   dataList?.forEach((item: any) => {
//     const materialItem: Record<string, any>[] = materialInfo?.[item?.materialID]
//     if (materialItem && materialItem?.length > 0) {

//     }
//   })
// }

/** OA详情监听渲染 */
const contractInfoEffect = (detailInfo: Record<string, any>, contractForm: any) => {
  /** 初始化数据 */
  useEffect(() => {
    if (detailInfo && detailInfo?.id) {
      const newDetailInfo: Record<string, any> = {
        ...detailInfo,
        contractName: detailInfo?.contractName,
        preBeginTime: detailInfo?.preBeginTime ? moment(detailInfo?.preBeginTime) : null,
        preFinishTime: detailInfo?.preFinishTime ? moment(detailInfo?.preFinishTime) : null,
        realBeginTime: detailInfo?.realBeginTime ? moment(detailInfo?.realBeginTime) : null,
        realFinishTime: detailInfo?.realFinishTime ? moment(detailInfo?.realFinishTime) : null,
        bidTime: detailInfo?.bidTime ? moment(detailInfo?.bidTime) : null,
        detailList: detailInfo?.detailList?.length > 0 ? handListMoment(detailInfo?.detailList, ['deliveryTime'], false) : [],
        processList: detailInfo?.processList?.length > 0 ? handListMoment(detailInfo?.processList, ['payTime'], false) : [],
        tempContractURL: detailInfo?.tempContractURL,
        contractURL: detailInfo?.contractURL,
        sumInTaxCN: detailInfo?.sumInTax ? changeNumMoneyToChinese(detailInfo?.sumInTax) : '零',
        createTime: detailInfo.createTime ? moment(detailInfo.createTime).format('YYYY-MM-DD') : null,
        capitalDirStr: detailInfo?.capitalDir == 0 ? '付款' : detailInfo?.capitalDir == 1 ? '收款' : detailInfo?.capitalDir == 2 ? '无金额' : '',
        oppositeListStr: '',
      };
      if (detailInfo?.oppositeList) {
        let arr: Record<string, any>[] = [];
        detailInfo?.oppositeList.map((item) => {
          arr.push(item?.oppositeName);
        });
        newDetailInfo.oppositeListStr = arr.join(',');
      }

      contractForm?.setFieldsValue({
        ...newDetailInfo,
      });
    }
  }, [detailInfo?.id]);
};

/** 处理合同履约节点数据 */
const handContractNodeList = (nodeList: Record<string, any>, info: Record<string, any>) => {
  let proceList: Record<string, any>[] = [];
  nodeList?.forEach((item: any) => {
    proceList?.push({
      ...item,
      capitalDir: item?.payFlag == 0 ? 2 : info?.capitalDir,
      payResidueSum: item?.payFinishSum ? item?.paySum - item?.payFinishSum : item?.paySum,
    });
  });
  return proceList;
};

/** 判断款项 */
const payTypeStr = (payType: number | string) => {
  if (payType == '0') {
    return '一次性支付';
  } else if (payType == '1') {
    return '预付款';
  } else if (payType == '2') {
    return '进度款';
  } else if (payType == '3') {
    return '尾款';
  } else if (payType == '4') {
    return '其他方式';
  } else if (payType == '5') {
    return '无款项';
  } else {
    return '--';
  }
};

/** 资金流向 */
const capitalDirType = (value: number | string) => {
  if (value == '0') {
    return '付款';
  } else if (value == '1') {
    return '收款';
  } else if (value == '2') {
    return '无金额';
  } else {
    return '--';
  }
};

/** 节点执行状态 */
const nodePayStateType = (value: number | string) => {
  if (value == '0') {
    return '待执行';
  } else if (value == '1') {
    return '执行中';
  } else if (value == '2') {
    return '部分执行';
  } else if (value == '3') {
    return '已完成';
  } else if (value == '4') {
    return '已取消';
  } else {
    return '--';
  }
};

export {
  handListMoment,
  handleDutySearchList,
  taxRateInfoHand,
  // busicDeptData,
  handSupplierNames,
  handMainContractData,
  // modifyContractCode,
  modifyJsonListFlag,
  effectMainContract,
  changePayList,
  defaultPayList,
  defaultDetailList,
  contractNumAndPayNum,
  handleNotPostion,
  handleAllDepartment,
  handleBookmark,
  contractInfoEffect,
  handContractNodeList,
  payTypeStr,
  capitalDirType,
  nodePayStateType,
  contractSumAndPaySum,
  changeContractSumList,
};
